/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.DrawController;
import controller.OverviewController;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.Timer;

/**
 *
 * @author Jasper
 */
public class GuessFrame extends javax.swing.JFrame{
    private DrawController drawController;
    private List<Point> points = new ArrayList<Point>();
    private Timer timer;
    private SwingWorker<List<Point>,Void> worker = new SwingWorker<List<Point>, Void>() {
    

        @Override
        protected List<Point> doInBackground() throws Exception {
            points.addAll(loadPoints());
            return points;
        }

        @Override
        protected void done() {
            //NOG ALLES WISSEN
            jPanel1.removeAll();
            Graphics g = jPanel1.getGraphics();
            
            //jPanel1.paintComponent(g);
            for(int i = 0; i < points.size() - 2; i++){
                Point p1 = points.get(i);
                g.drawOval(p1.x, p1.y, 1, 3);
            }
        }
        
        
    };
    private int number;
    private int loopslot;
    private int off;
    private int offset;
    private int maxWidth;
    
    protected List<Point> loadPoints() {
        return drawController.loadPoints();
    }
    
    /**
     * Creates new form GuessFrame
     */
    public GuessFrame(DrawController drawcontroller) {
        initComponents();
        this.drawController = drawcontroller;
        if(drawController.getClient().getSoort().equals("tekenaar")){
            btnRaden.setText("OK");
        }
        timer = new Timer(1000,new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //elke seconde --> herpainten en kijken of het spel al gedaan is
                doRepaint(drawController.loadPoints());
                isFinished();              
            }      
        });
        timer.start();
    }
        private void doRepaint(List<Point> loadPoints) {
            jPanel1.removeAll();
            Graphics g = jPanel1.getGraphics();
            for(Point p : loadPoints){
                g.drawOval(p.x, p.y, 1, 3);
            }
        }
        private void isFinished() {
                boolean finished = drawController.isFinished();
                if(finished){
                    timer.stop();
                    JOptionPane.showMessageDialog(this, "Helaas, iemand anders was u voor.");
                    this.setVisible(false);
                    new OverviewForm(new OverviewController(drawController.getClient())).setVisible(true);
                }
            }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new ImagePanel(drawController);
        btnRaden = new javax.swing.JButton();
        txtWoord = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 334, Short.MAX_VALUE)
        );

        btnRaden.setText("Raden");
        btnRaden.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                btnRadenMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(125, Short.MAX_VALUE)
                .addComponent(txtWoord, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(btnRaden)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRaden)
                    .addComponent(txtWoord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 28, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnRadenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnRadenMouseClicked
        //zenden naar server via controller
        String woord = txtWoord.getText();
        System.out.printf("%s --> server", woord);
        
        //INDIEN TEKENAAR --> WOORD ZENDEN
        boolean result = drawController.guessWord(drawController.getClient().getNaam(), woord);
        
        if(!result){
            btnRaden.setEnabled(false);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                Logger.getLogger(GuessFrame.class.getName()).log(Level.SEVERE, null, ex);
            }
            btnRaden.setEnabled(true);
            JOptionPane.showMessageDialog(this, "U heeft verkeerd geraden, probeer nog eens.");
        }else{
            JOptionPane.showMessageDialog(this, "Proficiat u heeft het woord geraden! U bent de winnaar!");
            timer.stop();
            this.setVisible(false);
            new OverviewForm(new OverviewController(drawController.getClient())).setVisible(true);
            
        }
        
    }//GEN-LAST:event_btnRadenMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GuessFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GuessFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GuessFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GuessFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new GuessFrame().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRaden;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField txtWoord;
    // End of variables declaration//GEN-END:variables
   
}
